import sm from 'sm-crypto'

import {hexToArray, arrayBufferToBase64} from "../utils/utils.js";

//SM4
const msg = '3338fe86fae013b5de903deae4033047528aa4b8713c8da97f1b984884d7e7bf' // 可以为 utf8 串或字节数组
const key = 'f4f79c5ead6685c40ea16aef81bf6d10' // 可以为 16 进制串或字节数组，要求为 128 比特

let encryptData1 = sm.sm4.encrypt(msg, key) // 加密，默认输出 16 进制字符串，默认使用 pkcs#7 填充（传 pkcs#5 也会走 pkcs#7 填充）
console.log("sm4.encryptData1: " + encryptData1)
let encryptData2 = sm.sm4.encrypt(msg, key, {
	padding: 'none'
}) // 加密，不使用 padding
console.log("sm4.encryptData2: " + encryptData2)
let encryptData3 = sm.sm4.encrypt(msg, key, {
	padding: 'none',
	output: 'array'
}) // 加密，不使用 padding，输出为字节数组
console.log("sm4.encryptData3: " + encryptData3)
let encryptData4 = sm.sm4.encrypt(msg, key, {
	mode: 'cbc',
	iv: 'fedcba98765432100123456789abcdef'
}) // 加密，cbc 模式
console.log("sm4.encryptData4: " + encryptData4)

let decryptData1 = sm.sm4.decrypt(encryptData1, key)
console.log("sm4.decryptData1: " + decryptData1)

const cipherMode = 1 // 1 - C1C3C2，0 - C1C2C3，默认为1

const msgString = '1c45b05f0da9486fa280a9d9a3acdb8a' // 可以为 utf8 串或字节数组
//去除前缀的04
const publicKey =
	'0413b0080ca43398e507cae6216f6f71ece5c442c75247c52fda5ae47c3c73d573a5683c8662c2d716f5f8da01979cd314a110e218b9844aea1ab51e931d90bfdb' // 可以为 16 进制串或字节数组，要求为 128 比特
//去除前缀的00
const privateKey = "00aa725a4f3c64394cb5004e83790ff03ce3bdacefb043f8eb4a63473ca1efeca7";

//SM2
let sm2encryptData1 = sm.sm2.doEncrypt(msgString, publicKey, cipherMode);

let a = '04'
let str = a.concat(sm2encryptData1)
//16进制
let tmp1 =
	'049dc81778ffa93b08c74c5cc9f64207b820f93645025bfa2b18da7e7b23dd81c4828a948c35ce379560afafe0f9c422a25968011efaa20eda198b551a9706ac5f490e2163da9a68a6bd4de0275bbb6e09e79622676e1e5c32e0ac643eea5a5606018451cb25867ea0c88e2b8c7574de2ccfb00a5900cd46a6f23578e758e13548'

let hexarray = hexToArray(tmp1)
console.log("hexToArray：" + hexarray)

let length = hexarray.length;
let buf = new ArrayBuffer(length)
let view = new Uint8Array(buf)
for (let i = 0; i < length; ++i) {
	view[i] = hexarray[i]
}
console.log("Uint8Array：" + view)
let base64 = arrayBufferToBase64(view)
console.log("arrayBufferToBase64：" + base64)